<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/lib_goods.php');
require_once(ROOT_PATH . '/' . ADMIN_PATH . '/includes/wtd.php');

$action = $_REQUEST['act']?$_REQUEST['act']:'list';

if ($action == 'list')
{
	admin_priv('stockage');
	
	$list = get_stockage_goods();
	
// 	print_r($list);
	$smarty->assign('list', $list['list']);
	assign_query_info();
	$smarty->assign('ur_here',          $_LANG['stockage']);
	$smarty->assign('full_page',    1);
	$smarty->assign('filter', $list['filter']);
	$smarty->assign('record_count',     $list['record_count']);
	$smarty->assign('page_count',      $list['page_count']);
	
	$smarty->display('stockage.htm');
}
elseif ($_REQUEST['act'] == 'query')
{
	$list = get_stockage_goods();
	
	// 	print_r($list);
	$smarty->assign('list', $list['list']);
	
	$smarty->assign('filter', $list['filter']);
	$smarty->assign('record_count',     $list['record_count']);
	$smarty->assign('page_count',      $list['page_count']);
	
	make_json_result($smarty->fetch('stockage.htm'), '',
			array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}
function get_stockage_goods()
{
	$whcode = 'FLC';

	if (isset($_REQUEST['page_size']) && intval($_REQUEST['page_size']) > 0)
	{
		$pageSize = intval($_REQUEST['page_size']);
	}
	elseif (isset($_COOKIE['ECSCP']['page_size']) && intval($_COOKIE['ECSCP']['page_size']) > 0)
	{
		$pageSize = intval($_COOKIE['ECSCP']['page_size']);
	}
	else
	{
		$pageSize = 15;
	}
	
	$data = array('whcode'=>$whcode,'pageSize'=>$pageSize,'pageNo'=>$_REQUEST['page'],'orderby'=>'');
	$result = query_stockage_batch($data);
	if ($result)
	{
		$filter['record_count'] = $result['total'];
		
		/* 分页大小 */
		$filter = page_and_size($filter);
		
		/* 查询 */
// 		$sql = "SELECT g.*,a.starttime,a.endtime FROM " . $GLOBALS['ecs']->table('goods') . " g LEFT JOIN " . $GLOBALS['ecs']->table('auto_manage') . " a ON g.goods_id = a.item_id AND a.type='goods'" . $where .
// 		" ORDER by goods_id, " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
// 		" LIMIT " . $filter['start'] . ",$filter[page_size]";
		
// 		$query = $GLOBALS['db']->query($sql);
		
// 		$goodsdb = array();
		
// 		while ($rt = $GLOBALS['db']->fetch_array($query))
// 		{
// 			if (!empty($rt['starttime']))
// 			{
// 				$rt['starttime'] = local_date('Y-m-d',$rt['starttime']);
// 			}
// 			if (!empty($rt['endtime']))
// 			{
// 				$rt['endtime'] = local_date('Y-m-d',$rt['endtime']);
// 			}
// 			$goodsdb[] = $rt;
// 		}
		
		$arr = array('list' => $result['stockages'], 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
	}
	else 
	{
		
	}
	
	
	return $arr;

}